<?php

namespace App\Models;


class CtcModel
{
    public static function CtcBuyList($conn,$CtcExample){
        if($CtcExample->size == 2){
            $join = "and `quantity` >= 1000";
        }else{
            $join = "and `quantity` < 1000";
        }
        $Sql="select `wid`,`price`,sum(`quantity`) as total_quantity from `ctc` where `wid` = $CtcExample->wid and `type`= 2 and status=1 $join group by `price` order by `price` desc limit $CtcExample->limit";
        return $conn->readConn($Sql,true);
    }
    public static function CtcSellList($conn,$CtcExample){
        if($CtcExample->size == 2){
            $join = "and `quantity` >= 1000";
        }else{
            $join = "and `quantity` < 1000";
        }
        $Sql="select `wid`,`price`,sum(`quantity`) as total_quantity from `ctc` where `wid` = $CtcExample->wid and `type`= 1 and status=1 $join group by `price` order by `price` asc limit $CtcExample->limit";
        return $conn->readConn($Sql,true);
    }
    public static function CtcDetailList($conn,$CtcExample){
        if($CtcExample->size == 2){
            $join = "and `quantity` >= 1000";
        }else{
            $join = "and `quantity` < 1000";
        }
        $Sql="select `id`,`wid`,`price`,`type`,`quantity` as total_quantity,`payment` from `ctc` where `uid` <> $CtcExample->uid and `wid` = $CtcExample->wid and `type`= $CtcExample->type and status=1 $join order by `create_time` asc limit $CtcExample->limit";
        return $conn->readConn($Sql,true);
    }
    /**
     * 查询发布单
     * entry $CtcExample
     * return int
     * */
    public static function getCtcDetailInit($conn, $CtcExample){
        $Sql="select `id`,`uid`,`wid`,`type`,`price`,`total_price`,`quantity`,`commission`,`commission_type`,`group_id`,`payment`,`status`,`finnshed_time`,`create_time`,`update_time` from `ctc` where id = $CtcExample->id";
        return $conn->readConn($Sql);
    }
    /**
     * 查询发布单
     * entry $CtcExample
     * return int
     * */
    public static function getCtcDetailInUid($conn, $CtcExample){
        $Sql="select `id`,`uid`,`wid`,`type`,`price`,`total_price`,`quantity`,`commission`,`commission_type`,`group_id`,`payment`,`status` from `ctc` where id = $CtcExample->id and `uid` = $CtcExample->uid";
        return $conn->readConn($Sql);
    }
    /**
     * 查询用户订单列表
     * entry $CtcExample
     * return int
     * */
    public static function getCtcUserList($conn, $CtcExample,$identical){
        $Sql="select `id`,`uid`,`wid`,`type`,`price`,`total_price`,`quantity`,`commission`,`commission_type`,`group_id`,`payment`,`status`,`finnshed_time`,`create_time`,`update_time` from `ctc` where `uid` = $CtcExample->uid and `status` = $CtcExample->status order by `create_time` desc limit $identical->pageStart,$identical->limit";
        return $conn->readConn($Sql,true);
    }
    /**
     * 查询当前币种挂单数量
     * entry $CtcExample
     * return int
     * */
    public static function getCtcNum($conn, $uid,$wid,$type){
        $Sql="select count(`id`) as num from `ctc` where `uid`= $uid and `wid` = $wid and `type` = $type and `status` in(1,2)";
        return $conn->readConn($Sql)["num"];
    }
    /**
     * 查询当前用户挂单数量
     * entry $CtcExample
     * return int
     * */
    public static function getCtcCount($conn, $uid){
        $Sql="select count(`id`) as num from `ctc` where `uid`= $uid and `status` in(1,2)";
        return $conn->readConn($Sql)["num"];
    }
    /**
     * 插入订单
     * entry $CtcExample
     * return int
     * */
    public static function addCtc($conn,$CtcExample){
        $Sql ="INSERT INTO `ctc`(`uid`, `wid`,`type`, `is_white`,`price`,`total_price`,`quantity`,`commission`,`group_id`,`payment`,`status`,`verify`,`create_time`,`update_time`) VALUES ('$CtcExample->uid','$CtcExample->wid','$CtcExample->type','$CtcExample->is_white','$CtcExample->price','$CtcExample->total_price','$CtcExample->quantity','$CtcExample->commission','$CtcExample->group_id','$CtcExample->payment','$CtcExample->status','$CtcExample->verify','$CtcExample->create_time','$CtcExample->update_time')";
        return $conn->executeConn($Sql);
    }
    /**
     * 修改店铺可添加字段
     * entry $CtcExample
     * return int
     * */
    public static function upCtcInside($conn,$CtcExample){
        $joinSql="";
        if($CtcExample->status != null){
            $joinSql.=" `status` = $CtcExample->status,";
        }
        if($CtcExample->update_time != null){
            $joinSql.=" `update_time` = $CtcExample->update_time,";
        }
        if($CtcExample->finnshed_time != null){
            $joinSql.=" `finnshed_time` = '$CtcExample->finnshed_time',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `ctc` SET $innerJoin WHERE `id` = '$CtcExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}